﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace project_math
{
    public partial class Form1 : Form
    {
        Items item = new Items();
        List<BinaryTree> li = new List<BinaryTree>();
        public Form1()
        {
            InitializeComponent();
            this.listView2.Hide();
            textBox3.Enabled = false;
            textBox4.Enabled = false;
        }
        //初始化    
        string address = AppDomain.CurrentDomain.BaseDirectory;
        private void button1_Click(object sender, EventArgs e)
        {
            
            this.listView2.Hide();
          

            int max_len;
            int max_span;
            int max_num;
            bool box1=false;
            bool box2=false;
            bool box3=false;
          
            this.errorProvider1.Clear();
            
            if (this.textBox1.Text.Trim().Length > 0)
            {
                if (int.TryParse(this.textBox1.Text, out max_len))
                {
                    
                    if (max_len <= 3 && max_len >= 1)
                    {
                       box1=true;
                    }
                    else
                    {
                        this.errorProvider1.SetError(this.textBox1, "要大于0小于4");
                    }
                }
                else
                {
                    this.errorProvider1.SetError(this.textBox1, "请输入数字");
                }
            } 
            else
            {
                this.errorProvider1.SetError(this.textBox1, "请输入范围");
            }
            if (this.textBox2.Text.Trim().Length > 0)
            {
                if (int.TryParse(this.textBox2.Text, out max_span))
                {
                    if (max_span > 0 )
                    {
                        box2 =true;
                    }
                    else
                    {
                        this.errorProvider1.SetError(this.textBox2, "请输入大于0的整数");
                    }
                }
                else
                {
                    this.errorProvider1.SetError(this.textBox2, "请输入数字");
                }
            }
            else
            {
                this.errorProvider1.SetError(this.textBox2, "请输入范围");
            }
            if (this.TISHU.Text.Trim().Length > 0)
            {
                if (int.TryParse(this.TISHU.Text, out max_num))
                { 
                    if (max_num > 0)
                    {
                       box3=true ;
                    }
                    else
                    {
                        this.errorProvider1.SetError(this.TISHU, "请输入大于0的整数");
                    }
                }
                else
                {
                    this.errorProvider1.SetError(this.TISHU, "请输入数字");
                }
            }
            else
            {
                this.errorProvider1.SetError(this.TISHU, "请输入范围");
            }
            if (box1 && box2 && box3)
            {
                max_len = int.Parse(this.textBox1.Text);
                max_span = int.Parse(this.textBox2.Text);
                max_num = int.Parse(this.TISHU.Text);

                this.listView1.Items.Clear();
                this.listView2.Items.Clear();
                List<BinaryTree> li = item.createQList(max_num, max_span, max_len);//生成题目 以树形式保存到li中
                this.li = li;
                foreach (BinaryTree s in li)
                {
                    this.listView1.Items.Add(s.getroot().s);
                    this.listView2.Items.Add(s.getroot().value.ToString());
                }


                System.IO.File.WriteAllText(address + "/Excises.txt", string.Empty);
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(address + "/Excises.txt", true))  //Grade.txt内容
                {
                    int i = 0;
                    foreach (BinaryTree line in li)
                    {
                        i++;
                        file.Write(i);
                        file.Write(',');
                        file.WriteLine(line.getroot().s.ToString()) ;// 直接追加文件末尾，换行 
                    }
                }
                System.IO.File.WriteAllText(address + "/Answers.txt", string.Empty);

                using (System.IO.StreamWriter file = new System.IO.StreamWriter(address + "/Answers.txt", true))  //Grade.txt内容
                {
                    int i = 0;　
                    foreach (BinaryTree line in li)
                    {
                        string s=line.getroot().value.ToString().Trim(new char[2] { '(',')' });
                        i++;
                        file.Write(i);
                        file.Write( ',' );
                        file.WriteLine(s);
                    }
                }
            }
          
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
       
            List<string> answer = new List<string>(); //回答
            List<string> re = new List<string>();    //Grade.txt
            List<string> aa = new List<string>();   
             OpenFileDialog file1 = new OpenFileDialog(); //打开文件管理窗口
            if (file1.ShowDialog() == DialogResult.OK)   //ShouDialog
            {
                
                StreamReader sr = File.OpenText(file1.FileName);
                StreamReader sd = File.OpenText(address+"/Answers.txt");
                while (sr.EndOfStream != true)
                {
                    answer.Add(sr.ReadLine());
                }

                sr.Close();

                while (sd.EndOfStream != true)
                {
                   aa.Add(sd.ReadLine());
                }

                sd.Close();
                int right = 0, error = 0;
                string Correct = "(";
                string Wrong = "(";
                for (int i = 0; i < li.Count; i++)
                {
                    if (i > answer.Count - 1)
                    {
                        ;
                    }
                    if (aa[i].Equals(answer[i]))
                    {
                        right++;
  
                        Correct = Correct + (i+1) + ",";
                    }
                    else
                    {
                        error++;
                        Wrong = Wrong + (i+1) + ",";
                    }
                }

                Correct=Correct.Substring(0, Correct.Length - 1);
                Correct = Correct + ")";
                Wrong=Wrong.Substring(0, Wrong.Length - 1);
                Wrong = Wrong + ")";
                this.textBox3.Text = right.ToString();
                this.textBox4.Text = error.ToString();
                this.comboBox1.Items.Clear();


                for (int i = 0; i < li.Count; i++)
                {
                    int count = 0;
                    for (int j = i + 1; j < li.Count; j++)
                    {
                        int result = item.compareTree(li[i], li[j]);
                        if (result == 0)
                        {
                            count++;
                            re.Add("(" + count + ")" + i + "," + li[i] + "repeat" + j + "," + li[j]);
                        }
                    }
                }
                foreach (string r in re)
                {
                    this.comboBox1.Items.Add(r);
                }


             

                System.IO.File.WriteAllText(address+"/Grade.txt", string.Empty);  //清空
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(address + "/Grade.txt", true))  //Grade.txt内容
                {
                    file.Write("Correct:");
                    file.WriteLine(Correct);
                    file.Write("Wrong:");
                    file.WriteLine(Wrong);
                    string a = "Repeat:" + re.Count;
                    file.WriteLine("RepeatDetail:");
                    file.WriteLine(a);
                    foreach (string line in re)
                    {
                        file.WriteLine(line);// 直接追加文件末尾，换行 
                    }
                }

            }
            else
            {
                MessageBox.Show("未找到文件");
            }
            this.listView2.Show();
        }

     
    }
}